Thread: [Help] About reverse string

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Unhappy [Help] About reverse string

    I must Write a program using getchar() function only to read the character string from the keyboard until ‘.’ is terminated, then reverse the string and display result on on the screen.

    I don't know why but my program doesn't run right. Can anyone help me, pls. And this is my code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    
    main()
    {
       unsigned int choice, i, j, n;
       char c, temp;
       char s1[100], s2[100];
       while(1){
       do{
           system("cls");
           printf("Choose one of the following options: ");
           printf("\n1. Input a string");
           printf("\n2. Dislay a string");
           printf("\n3. Reverse a string");
           printf("\n0. Exit");
           printf("\nYour selection (0 -> 3): ");
           scanf("%d", &choice);}
       while(choice<0 || choice>3);
       switch(choice){
                         case 1: printf("\nEnter string ended by . : ");
                                 for(i=0; (c=getchar()) != '.'; i++) s1[i]=c;
                                 n=strlen(s1);
                                 for(i=0;i<n;i++)
                                    if (s1[i]=='\n') 
                                        {
                                           for(j=i;j<n;j++) s1[j]=s1[j+1];
                                           s1[n--]='\0';
                                           i--;
                                        }
                                 strcpy(s2,s1);
                                 system("pause");
                                 break;
                         case 2: printf("String entered is \" ");
                                 for(i=0;i<n;i++) printf("%c", s1[i]);
                                 printf(" \" \n");
                                 system("pause");
                                 break;
                         case 3: for(i=0;i<n-1;i++)
                                    for(j=i+1;j<n;j++)
                                    {
                                       temp=s2[i];
                                       s2[i]=s2[j];
                                       s2[j]=temp;
                                    };
                                 printf("The orginal string: ");
                                 for(i=0;i<n;i++) printf("%c", s1[i]);
                                 printf("\n");
                                 printf("The reverse string: ");
                                 for(i=0;i<n;i++) printf("%c", s2[i]);
                                 printf("\n");
                                 system("pause");
                                 break;
                         case 0: exit(0);}}
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    16
    I have fixed my program.
    It should have "fflush(stdin)" in case 1

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Erm, nope - you should have read the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Congratulations on being entirley dependant on C-according-to-Borland-from-ye-olde-days-for-Windows. Bleh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM